import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

public class SearchComparison {
    public static void sortComparison(String[] fileNames) {
        try {
            FileWriter csvWriter = new FileWriter("C:\\Users\\ASUS\\Desktop\\sortComparison.csv");
            // 写入标题行
            csvWriter.append(", 10, 100, 10000\n");
            csvWriter.append("bubbleSort");

            for (String fileName : fileNames) {
                int[] data = generateRandomData(Integer.parseInt(fileName.replaceAll("[^0-9]", "")));

                // 对冒泡排序进行多次测试并取平均值
                long totalBubbleSortTime = 0;
                for (int i = 0; i < 10; i++) {
                    long startTime = System.currentTimeMillis();
                    measureBubblePortal(data.clone());
                    long endTime = System.currentTimeMillis();
                    totalBubbleSortTime += (endTime - startTime);
                }
                long averageBubbleSortTime = totalBubbleSortTime / 10;
                csvWriter.append(",");
                csvWriter.append(String.valueOf(averageBubbleSortTime));
            }

            csvWriter.append("\nmergeSort");

            for (String fileName : fileNames) {
                int[] data = generateRandomData(Integer.parseInt(fileName.replaceAll("[^0-9]", "")));

                // 对归并排序进行多次测试并取平均值
                long totalMergeSortTime = 0;
                for (int i = 0; i < 10; i++) {
                    long startTime = System.currentTimeMillis();
                    measureMergeSort(data.clone());
                    long endTime = System.currentTimeMillis();
                    totalMergeSortTime += (endTime - startTime);
                }
                long averageMergeSortTime = totalMergeSortTime / 10;
                csvWriter.append(",");
                csvWriter.append(String.valueOf(averageMergeSortTime));
            }

            csvWriter.flush();
            csvWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 生成随机数据数组
    private static int[] generateRandomData(int size) {
        Random random = new Random();
        int[] data = new int[size];
        for (int i = 0; i < size; i++) {
            data[i] = random.nextInt(1000); // 生成0到999之间的随机数，可根据需要调整范围
        }
        return data;
    }

    private static int[] readDataFromFile(File file) {
        try {
            Scanner scanner = new Scanner(file);
            StringBuilder dataBuilder = new StringBuilder();
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                for (int i = 0; i < line.length(); i++) {
                    if (Character.isDigit(line.charAt(i))) {
                        dataBuilder.append(line.charAt(i));
                    }
                }
                dataBuilder.append(" ");
            }
            scanner.close();

            String[] dataArray = dataBuilder.toString().split(" ");
            int[] data = new int[dataArray.length];
            for (int i = 0; i < dataArray.length; i++) {
                if (!dataArray[i].isEmpty()) {
                    data[i] = Integer.parseInt(dataArray[i]);
                }
            }
            return data;
        } catch (IOException e) {
            e.printStackTrace();
            return new int[0];
        }
    }

    private static void measureBubblePortal(int[] data) {
        // 简单的冒泡排序实现
        for (int i = 0; i < data.length - 1; i++) {
            for (int j = 0; j < data.length - i - 1; j++) {
                if (data[j] > data[j + 1]) {
                    int temp = data[j];
                    data[j] = data[j + 1];
                    data[j + 1] = temp;
                }
            }
        }
    }

    private static void measureMergeSort(int[] data) {
        // 简单的归并排序实现
        if (data.length > 1) {
            int mid = data.length / 2;
            int[] left = new int[mid];
            int[] right = new int[data.length - mid];

            for (int i = 0; i < mid; i++) {
                left[i] = data[i];
            }
            for (int i = mid; i < data.length; i++) {
                right[i - mid] = data[i];
            }

            measureMergeSort(left);
            measureMergeSort(right);

            int i = 0, j = 0, k = 0;
            while (i < left.length && j < right.length) {
                if (left[i] < right[j]) {
                    data[k++] = left[i++];
                } else {
                    data[k++] = right[j++];
                }
            }
            while (i < left.length) {
                data[k++] = left[i++];
            }
            while (j < right.length) {
                data[k++] = right[j++];
            }
        }
    }
}

SearchComparison.sortComparison(new String[]{"sort10.txt", "sort100.txt", "sort10000.txt"})

// 定义自定义比较器
int compare(String a, String b) {
    // 定义花色的优先级
    String suits = "HCDS";  // 花色的优先级: H < C < D < S
    // 提取卡牌的花色和数字部分
    int numA = Integer.parseInt(a.substring(0, a.length() - 1)); // 数字部分
    int numB = Integer.parseInt(b.substring(0, b.length() - 1)); // 数字部分
    char suitA = a.charAt(a.length() - 1); // 花色部分
    char suitB = b.charAt(b.length() - 1); // 花色部分
    
    // 首先按花色进行比较
    int suitComparison = Integer.compare(suits.indexOf(suitA), suits.indexOf(suitB));
    if (suitComparison != 0) {
        return suitComparison;
    }
    
    // 如果花色相同，按数字部分比较
    return Integer.compare(numA, numB);
}

// 定义冒泡排序方法
void bubbleSort(ArrayList<String> list) {
    int n = list.size();
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (compare(list.get(j), list.get(j + 1)) > 0) {
                // 交换元素
                String temp = list.get(j);
                list.set(j, list.get(j + 1));
                list.set(j + 1, temp);
            }
        }
    }
}

// 创建并初始化卡牌列表
ArrayList<String> list = new ArrayList<>(List.of("4H", "3S", "7S", "8C", "2D", "3H"));

// 执行排序
bubbleSort(list);

// 输出排序后的结果
list

ArrayList<Integer> cardCompare(String card1, String card2) {
    // 将卡片分为数字和花色
    int number1 = Integer.parseInt(card1.substring(0, card1.length() - 1));
    int number2 = Integer.parseInt(card2.substring(0, card2.length() - 1));
    char suit1 = card1.charAt(card1.length() - 1);
    char suit2 = card2.charAt(card2.length() - 1);

    // 定义花色优先级
    String suitPriority = "HCDS";

    // 比较花色
    if (suitPriority.indexOf(suit1) < suitPriority.indexOf(suit2)) {
        return new ArrayList<>(List.of(-1));
    } else if (suitPriority.indexOf(suit1) > suitPriority.indexOf(suit2)) {
        return new ArrayList<>(List.of(1));
    } else {
        // 如果花色相同，则比较数字
        if (number1 < number2) {
            return new ArrayList<>(List.of(-1));
        } else if (number1 > number2) {
            return new ArrayList<>(List.of(1));
        } else {
            return new ArrayList<>(List.of(0));
        }
    }
}

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

List<Integer> readIntFile(String filePath) {
    List<Integer> lines = new ArrayList<>();
    try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
        String line;
        while ((line = br.readLine())!= null) {
            try {
                int num = Integer.parseInt(line.trim());
                lines.add(num);
            } catch (NumberFormatException ignored) {
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return lines;
}

{
    long startTime = System.currentTimeMillis();
    List<Integer> dataList = readIntFile("C:\\Users\\ASUS\\Desktop\\coursework2_files\\sort10.txt");
    Integer[] dataArray = dataList.toArray(new Integer[0]);
    if (dataArray.length > 0) {
        long sortStartTime = System.currentTimeMillis();
        java.util.Arrays.sort(dataArray);
        long sortEndTime = System.currentTimeMillis();
        System.out.println("排序所用时间（毫秒）: " + (sortEndTime - sortStartTime));
    } else {
        System.out.println("排序所用时间（毫秒）: 0");
    }
    long endTime = System.currentTimeMillis();
}